<?php

namespace My\Controller;

use Utuo\BackgroundController;

/**
 * wmfl.cn version 5.0
 * -----------------------------------------------
 *
 * @author esinger <esinger@qq.com>
 * @link http://www.utuo88.com
 * @copyright 2014 Utuo Tech co.,Ltd
 */

/**
 * 客户后台主页面
 */
class IndexController extends BackgroundController {
	// 初始化
	public function _initialize() {
		// 管理页面配置
		$this->getManagerConfig();
		$this->setControllerTitle('后台首页');
		$this->setActionTitle('home', '欢迎页面');
		$this->setActionTitle(array(
		    'home' => '欢迎',
		    'money' => '充值',
		    'pv' => '流量'
		));
		$this->setActionNav('home,money,pv');
		$this->setPathVisible(false);
	} 
	
	// 后台首页
	public function indexAction() {
		$userid = session("userid") ? session('userid') : 0;
		// 顶级菜单
	    $topMenu = M('customerMenu a')->join('__CUSTOMER_MENU_MANAGER__ b on a.id=b.menu_id')->where('a.pid=0 AND a.is_deleted=0 AND a.status=1 AND b.admin_cid=%d ', $userid)->field("a.*")->order('a.sort')->select();
		// 快捷菜单
		$shortcutMenu = M('customerMenu a')->join('__CUSTOMER_MENU_MANAGER__ b on a.id=b.menu_id')->where('a.pid='.$topMenu[0]['id'].' AND a.is_deleted=0 AND a.status=1 AND b.admin_cid=%d', $userid)->field("a.*")->order('a.sort')->select();
		// 客户后台显示合作商的信息
		// 暂时这样处理，还有更好的方式
		$pid = I('pid',0,'intval');
		if($pid){
		  $partner = M('Partner')->find($pid);
		  session('loginPartner',$partner);
		  $this->assign('partner',$partner);
		  layout($partner['label']);
		} else {
		    layout('layout');
		}
		$this->assign('shortcutMenu', $shortcutMenu);
		$this->assign('topMenu', $topMenu);
		$this->assign('siteUrl', get_site_url($this->aid));
		$this->assign("sid", $this->aid);
		
		$this->display();
	}
	
	
	// 获取子菜单
	public function getSubMenuAction() {
		$pid = I('pid', 0, 'intval');
		$userid = session("userid") ? session('userid') : 0;
		$subMenu = M('customerMenu a')->join('__CUSTOMER_MENU_MANAGER__ b on a.id=b.menu_id')->where(array(
				'a.pid' => $pid,
				'a.is_deleted' => 0,
				'a.status' => 1,
				'b.admin_cid' => $userid,
				
		))->order('a.sort')->select();
		
		$this->ajaxReturn($subMenu);
	}
	
	// 内容首页
	public function homeAction() {
	    $customer = M('Customer')->find($this->aid);
	    $config = M('Config')->find();
	    
	    // 总充值
	    $totalMoney = M('CustomerFinance')->where('cid=%d',$this->aid)->sum('money');
	    $customer['totalMoney'] = $totalMoney ? $totalMoney : 0;
	     
	    // 平均日流量
	    $pvDays = M('CustomerReport')->where("cid=%d",$this->aid)->count();
	    $customer['pv_avg'] = ceil($customer['pv']/$pvDays);
	    
	    // 最近7天流量记录
	    $reports = M('CustomerReport')->where(array('cid'=>$this->aid))->limit(7)->order("id DESC")->select();
	    
	     
	    if ($pvDays<=7){
	        $customer['pv_week_avg'] = $customer['pv_avg'];
	    } else {
    	    // 最近7天的总流量
    	    $pvWeekCount = 0;
    	    foreach ($reports as $v){
    	        $pvWeekCount+= $v['pv'];
    	    }
	        $customer['pv_week_avg'] = ceil($pvWeekCount/7);
	    }
	    $customer['pv_rest'] = $customer['pv_count'] + $customer['pv_default'] - $customer['pv'];
	    $customer['day_rest'] = $customer['pv_week_avg'] ? number_format($customer['pv_rest'] / $customer['pv_week_avg'],1) : '>9999';
	    
	    $this->assign('config',$config);
	    $this->assign('reports',$reports);
	    $this->assign('customer',$customer);
	    $this->assign('sid',$this->aid);
	    $this->assign('pid', session('pid'));
	    
	    // 客户后台显示合作商的信息
	    // 暂时这样处理，还有更好的方式
	    $partner = session('loginPartner');
	    if ($partner){
	        $this->assign('partnerConfig',$partner);
	        $this->display($partner['label']);
	    } else {
		    $this->display();
	    }
	}
	
	// 充值
	public function moneyAction(){
	    $model = M('CustomerFinance');
	    $model->where(array(
	        'cid' => $this->aid
	    ));
	     
	    $clone = clone $model;
	    $count = $clone->count();
	     
	    $page = $this->getPage();
	    $pagesize = 30;
	    $lists = $model->limit($pagesize)->page($page)->order("id DESC")->select();
	    
	    $this->assign('lists',$lists);
	    $this->showPage($count, $pagesize);
	    $this->display();
	}
	
	// PV流量
	public function pvAction(){
	    $model = M('CustomerReport');
	    $model->where(array(
	        'cid' => $this->aid
	    ));
	    
	    $clone = clone $model;
	    $count = $clone->count();
	    
	    $page = $this->getPage();
	    $pagesize = 30;
	    $lists = $model->limit($pagesize)->page($page)->order("id DESC")->select();
	    
	    $this->assign('lists',$lists);
	    $this->showPage($count, $pagesize);
	    $this->display();
	}
}
 
// End of file IndexController.class.php
